package com.yiren.manager.ctrl.messhall;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAllowanceApply;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.messhall.YrAllowanceApplyForm;
import com.yiren.manager.service.iface.messhall.YrAllowanceApplyService;
import com.yiren.manager.utils.ObjectUtils;

/**
 * 补贴申请 
 * @author Administrator
 *
 */
@RequestMapping("allowanceApply")
@Controller
public class YrAllowanceApplyController extends GenericController<YrAllowanceApplyForm, YrAllowanceApply>{

	@Autowired
	private YrAllowanceApplyService selfService;
	
	@RequestMapping("save")
	@ResponseBody
	public HTTPResult<YrAllowanceApplyForm> save(String data) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MyException, SwallowException{
		HTTPResult<YrAllowanceApplyForm> result = new HTTPResult<YrAllowanceApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrAllowanceApplyForm form = tool.mapper("allowanceApply", YrAllowanceApplyForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getAllowanceApplyId()||form.getAllowanceApplyId().isEmpty()){
			form.setAllowanceApplyId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
			form.setApproveStatus(0);//设置为未审批状态
			form.setApplyDate(new Date());
		}
		form.setUpdateTime(new Date());
		YrAllowanceApply entity = ObjectUtils.objectToObject(form, YrAllowanceApply.class);
		selfService.save(entity);
		List<YrAllowanceApplyForm> formList = new ArrayList<YrAllowanceApplyForm>();
		formList.add(form);
		result.setData(formList);
		
		return result;
	}

	/**
	 * 审批
	 * @param id
	 * @param approveStatus
	 * @return
	 * @throws MyException
	 */
	@RequestMapping("approve")
	@ResponseBody
	public HTTPResult<YrAllowanceApplyForm> approve(String id,Integer approveStatus) throws MyException{
		HTTPResult<YrAllowanceApplyForm> result = new HTTPResult<YrAllowanceApplyForm>();
		if(null==id||null==approveStatus){
			throwExp("参数有误");
		}
		YrAllowanceApply allowanceApply = selfService.findOne(id);
		if(null==allowanceApply){
			throwExp("查询不到对应的申请记录");
		}
		allowanceApply.setApproveStatus(approveStatus);
		selfService.save(allowanceApply);
		
		return result;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrAllowanceApplyForm> findAll() throws Exception{
		HTTPResult<YrAllowanceApplyForm> result = new HTTPResult<YrAllowanceApplyForm>();
		String sqlWhere = "approve_status=0";
		QueryResult<YrAllowanceApply> qr = selfService.find(YrAllowanceApply.class, sqlWhere,new Object[]{});
		List<YrAllowanceApplyForm> formList = new ArrayList<YrAllowanceApplyForm>();
		for(YrAllowanceApply entity : qr.getResultList()){
			formList.add(toForm(entity));
		}
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrAllowanceApplyForm toForm(YrAllowanceApply e) throws Exception {
		YrAllowanceApplyForm form = ObjectUtils.objectToObject(e, YrAllowanceApplyForm.class);
		return form;
	}

	@Override
	public YrAllowanceApply toEntity(YrAllowanceApplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


}
